package com.zhuss.${moudle}.processor;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.${moudle}.${tablePkg}.Create${poEntity}Bo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.${moudle}.db.dao.ext.${poEntity}Dao;
import com.zhuss.${moudle}.db.entity.${poEntity};
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.OceanException;
import com.zhuss.util.JsonUtil;


/**
 * @description: ${tableComment}创建
 * @author: zhushanshuo
 * @create: ${creatTime}
 **/
@Component("${poEntityLower}Create")
public class ${poEntity}Create extends AbstractProcessor<Object, Create${poEntity}Bo> implements Processor<Object, Create${poEntity}Bo> {

    @Autowired
    private ${poEntity}Dao ${poEntityLower}Dao;
    
    @Autowired
	@Qualifier("${poEntityLower}Read")
	private ${poEntity}Read ${poEntityLower}Read;
    
    
    /**
     * 校验
     */
    @Override
    public void check(Create${poEntity}Bo bo) throws OceanException {
    	// TODO 创建 ${tableComment} 校验， 请补充
    	String name = bo.getName();
    	if (name == null || "".equals(name.trim())) {
    		// throw new OceanException(ExceptionCode.${tableComment}name为空);
    	}
    	
    	List<${poEntity}> list = ${poEntityLower}Read.list(name);
    	if (!list.isEmpty()) {
    		// throw new OceanException(ExceptionCode.${tableComment}name已存在);
    	}
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(Create${poEntity}Bo param) {
    	${poEntity} record = new ${poEntity}();
${createProcessSetParam}
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	${poEntityLower}Dao.create(record);
    	param.setTargetId(record.get${poEntity}Id());
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(Create${poEntity}Bo param) throws OceanException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.默认模块请修改.key());
    	record.setAction(LogActionEnum.${tableComment}创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
